package model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import java.util.HashMap;

public class JsonBehaviour extends WCFBehaviour
{
	@Override
	public HashMap<String, Machine> translateResponseToMachine(String response) 
	{
		Log.d("JSON", String.format("JSONBehaviour - translateResponse: %s", response));
		HashMap<String, Machine> machines = new HashMap<String, Machine>();
		
		try 
		{
			JSONArray json = new JSONArray(response);
			
			for (int i = 0; i < json.length(); i++) 
			{
				JSONObject obj = json.getJSONObject(i);
				machines.put(obj.getString("ID"), createMachine(obj.getString("ID"),obj.getString("MachineName"),
						obj.getString("Part"),obj.getString("Company"), obj.getString("Description"), obj.getString("CurrentStatus")));
			}
		} catch (JSONException e) 
		{
			e.printStackTrace();
		}
		return machines;
	}
}
